﻿
using System.Diagnostics;
using SqlSugar;

namespace DADAL
{
    public class SqlSugarHelper
    {
        /// <summary>
        /// key
        /// </summary>
        public static string EncryptionKey = "datakeys";

        /// <summary>联接数据库
        /// 
        /// </summary>
        static string DBPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Collect.db");

        static string ConnectionString = "Data Source=" + DBPath;

        /// <summary>错误代码
        /// 
        /// </summary>
        static int ErrorCode = -1;

        public static SqlSugarScope DB = new SqlSugarScope(new ConnectionConfig()
        {
            ConnectionString = ConnectionString,//连接符字串
            DbType = DbType.Sqlite,//数据库类型
            IsAutoCloseConnection = true,
            InitKeyType = InitKeyType.Attribute,
        },
            db =>
            {
                db.Aop.OnLogExecuting = (sql, pars) =>
                {
                    Debug.WriteLine(sql);//输出sql,查看执行sql 性能无影响
                };
                db.Aop.OnError = (exp) =>//SQL报错
                {
                    Console.WriteLine("SQL执行错误：【" + exp.Message + "】");
                };
            }
         );

        /// <summary>连接状态
        /// 
        /// </summary>
        /// <returns></returns>
        public static bool State()
        {
            FileInfo info = new FileInfo(DBPath);
            bool exists = info.Exists;
            if (exists == false)
            {
                return false;
            }
            else
            {

                return DB.Ado.IsValidConnection();
            }
        }

        /// <summary>插入数据
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int Insertable<T>(List<T> list) where T : class, new()
        {
            try
            {
                int res = DB.Insertable(list).ExecuteCommand();
                return res;
            }
            catch (Exception ex)
            {
                Debug.WriteLine("插入记录异常：" + ex.Message);
                return ErrorCode;
            }
        }

        /// <summary>删除记录
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <returns></returns>
        public static int Deleteable<T>(List<T> list) where T : class, new()
        {
            try
            {
                return DB.Deleteable<T>(list).ExecuteCommand();
            }
            catch (Exception ex)
            {
                Debug.WriteLine("删除记录异常：" + ex.Message);
                return ErrorCode;
            }
        }

        /// <summary>更新记录
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int Updateable<T>(List<T> list) where T : class, new()
        {
            try
            {
                return DB.Updateable<T>(list).ExecuteCommand();
            }
            catch (Exception ex)
            {
                Debug.WriteLine("更新记录异常：" + ex.Message);
                return ErrorCode;
            }
        }

        /// <summary>查询符合条件的记录
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="where"></param>
        /// <returns></returns>
        public static List<T> Inquireable<T>(Dictionary<string, Object>? where)
        {
            try
            {
                ISugarQueryable<T>? sugarQueryable = null;
                if (where != null)
                {
                    sugarQueryable = DB.Queryable<T>().WhereColumns(where);
                }
                else
                {
                    sugarQueryable = DB.Queryable<T>();
                }
                List<T> list = sugarQueryable.ToList();
                return list;
            }
            catch (Exception ex)
            {
                Debug.WriteLine("查询记录异常：" + ex.Message);
                return new List<T>();
            }
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="where"></param>
        /// <returns></returns>
        public static List<T> InquireableToPageList<T>(Dictionary<string, Object>? where, int pageNumber, int pageSize, ref int totalCount)
        {
            try
            {
                ISugarQueryable<T>? sugarQueryable = null;
                if (where != null)
                {
                    sugarQueryable = DB.Queryable<T>().WhereColumns(where);
                }
                else
                {
                    sugarQueryable = DB.Queryable<T>();
                }
                List<T> list = sugarQueryable.ToPageList(pageNumber, pageSize, ref totalCount);
                return list;
            }
            catch (Exception ex)
            {
                Debug.WriteLine("查询记录异常：" + ex.Message);
                return new List<T>();
            }
        }
    }
}
